package cn.display.pixelworksx7.adapter

import android.content.pm.ApplicationInfo
import android.content.pm.PackageInfo
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import cn.display.pixelworksx7.R
import cn.display.pixelworksx7.constant.GlobalData
import cn.display.pixelworksx7.data.AppConfigInfo
import cn.display.pixelworksx7.threadpool.SystemThreadPool
import cn.display.pixelworksx7.util.IrisConfigUtil
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.util.Objects

class RecyclerViewBaseAdapter(var packageInfoList: MutableList<PackageInfo>) :
    RecyclerView.Adapter<RecyclerViewBaseAdapter.IrisConfigViewHolder>() {
    private lateinit var onItemClickListener: OnItemClickListener
    private val TAG = "RecyclerViewBaseAdapter"
    class IrisConfigViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        //获取子条目的布局控件ID
      val versionTextView: TextView = view.findViewById(R.id.version)
        var appNameTextView: TextView = view.findViewById(R.id.appName)
        var appIcon: ImageView = view.findViewById(R.id.appIcon)
//        fun bindIrisConfig (appConfigInfo : ApplicationInfo) {
//       //     packageNameTextView.text = appConfigInfo.packageName
//            appNameTextView.text = appConfigInfo.loadLabel(GlobalData.packageManager)
//            appIcon.setImageDrawable(appConfigInfo.loadIcon(GlobalData.packageManager))
//        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewBaseAdapter.IrisConfigViewHolder {
        val itemView = LayoutInflater.from(parent.context)
            .inflate(R.layout.iris_config_view, parent, false)
        return IrisConfigViewHolder(itemView)
    }

    override fun onBindViewHolder(holder: RecyclerViewBaseAdapter.IrisConfigViewHolder, position: Int) {
        var packageInfo = packageInfoList.get(position)
        holder.appNameTextView.text = packageInfo.applicationInfo.loadLabel(GlobalData.packageManager)
        // 使用BitmapDrawable将Bitmap转换为Drawable
        if (File(GlobalData.storageDir.absolutePath + "/" + packageInfo.packageName + ".png").exists()) {
            holder.appIcon.setImageBitmap(BitmapFactory.decodeFile(GlobalData.storageDir.absolutePath + "/" + packageInfo.packageName + ".png"))
        } else {
            val icon = packageInfo.applicationInfo.loadIcon(GlobalData.packageManager)
            holder.appIcon.setImageDrawable(icon)
            val bitmap: Bitmap = IrisConfigUtil.drawableToBitmap(icon)
            val fileName = "${packageInfo.packageName}.png"
            val file = File(GlobalData.storageDir, fileName)
            try {
                FileOutputStream(file).use { fos ->
                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos)
                }
                println("Icon for ${packageInfo.packageName} saved to $file")
            } catch (e: IOException) {
                e.printStackTrace()
            }
        }
        Log.d(TAG, "GlobalData.storageDir.absolutePath" + GlobalData.storageDir.absolutePath)
        holder.versionTextView.text = packageInfo.versionName
        var viewList: List<View> = mutableListOf<View>(holder.itemView.findViewById(R.id.app_info_view))
        for (item in viewList) {
            var view = item as View
            view.setOnClickListener {
                onItemClickListener?.onItemClick(view, position)
            }
            view.setOnLongClickListener {
                onItemClickListener?.onItemLongClick(view, position)
                true
            }
        }
    }

    override fun getItemCount(): Int {
        return packageInfoList.size
    }

    fun setData(items: MutableList<PackageInfo>) {
        packageInfoList = items
        notifyDataSetChanged()
    }

    fun updateItems(newItems: List<PackageInfo>) {
        Log.d(TAG, "刷新list")
        packageInfoList.clear()
        packageInfoList.addAll(newItems)
        notifyDataSetChanged()
    }

    fun addItems(newItems: MutableList<PackageInfo>) {
        packageInfoList.addAll(newItems)
        notifyDataSetChanged()
    }

    fun setOnItemClickListener(listener: OnItemClickListener) {
        this.onItemClickListener = listener
    }

    interface OnItemClickListener {
        fun onItemClick(view: View, position: Int)
        fun onItemLongClick(view: View, position: Int)
    }
}

